﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CtpApi;
using CtpApi.Struct;

using WAF.DAO;
using WAF.EntityFramework;

using TraderClient.Models;

namespace TraderClient.Views
{
    public partial class InstrumentInfo : Form
    {
        public InstrumentInfo()
        {
            InitializeComponent();
        }

        private void InstrumentInfo_Load(object sender, EventArgs e)
        {
            string sqlProductID = "SELECT ProductID FROM Instrument GROUP BY ProductID";
            DataTable productIDTable = DbAccess.ExeDataSet(sqlProductID).Tables[0];

            foreach (DataRow productRow in productIDTable.Rows)
            {
                string productID = productRow["ProductID"].ToString();
                TreeNode productNode = new TreeNode(productID);

                string sql = "SELECT Id, InstrumentID, InstrumentName FROM Instrument WHERE ProductID='" + productID + "' ORDER BY InstrumentID";
                DataTable tb = DbAccess.ExeDataSet(sql).Tables[0];

                foreach (DataRow row in tb.Rows)
                {
                    TreeNode childNode = new TreeNode();
                    
                    childNode.Text = row["InstrumentID"].ToString() + "(" + row["InstrumentName"].ToString() + ")";
                    childNode.Name = row["InstrumentID"].ToString();

                    productNode.Nodes.Add(childNode);
                }

                treeViewInstrument.Nodes.Add(productNode);
            }
        }

        private void treeViewInstrument_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {

        }

        private void treeViewInstrument_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Name != "")
            {
                string sqlInstrumentInfo = "SELECT * FROM Instrument WHERE InstrumentID='" + e.Node.Name + "'";
                DataTable tb = DbAccess.ExeDataSet(sqlInstrumentInfo).Tables[0];

                DataRow row = tb.Rows[0];

                labelInstrumentID.Text = "合约代码：" + row["InstrumentID"].ToString();
                labelInstrumentName.Text = "合约名称：" + row["InstrumentName"].ToString();
                labelPriceTick.Text = "最小变动价格：" + row["PriceTick"].ToString();
                labelVolumeMultiple.Text = "每手数量：" + row["VolumeMultiple"].ToString();
                labelLongMarginRatio.Text = "多头保证金：" + row["LongMarginRatio"].ToString();
                labelShortMarginRatio.Text = "空头保证金：" + row["ShortMarginRatio"].ToString();
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            EntitySession session = new EntitySession();
            session.OpenConnection();
            session.BeginTransaction();

            try
            {
                session.DbAccess.ExecuteNonQuery("DELETE FROM DisplayInstruments");

                foreach (TreeNode node in treeViewInstrument.Nodes)
                {
                    foreach (TreeNode childNode in node.Nodes)
                    {
                        if (childNode.Checked)
                        {
                            DisplayInstruments entity = new DisplayInstruments{ InstrumentID = childNode.Name };
                            session.Insert(entity);
                        }
                    }
                }

                session.CommitTtransaction();
            }
            catch(Exception ex)
            {
                session.RollbackTtransaction();
                MessageBox.Show(ex.Message);
            }
            finally
            {
                session.CloseConnection();
            }

            this.Close();
        }
    }
}
